package com.blacktech.dbu.meta.entity;

import com.blacktech.dbu.core.common.BaseEntity;
import com.blacktech.dbu.core.dict.ColumnType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 字段库实体
 * 用于统一管理字段定义，确保同一产品下的字段属性一致性
 * 支持产品级隔离，不同产品维护独立的字段库
 *
 * @author Yanyunsong
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "dbu_field_library",
       uniqueConstraints = @UniqueConstraint(columnNames = {"product_id", "field_name_en"}))
public class DbuFieldLibrary extends BaseEntity {

    /**
     * 关联的产品ID
     */
    @Column(name = "product_id", nullable = false)
    private Long productId;

    /**
     * 关联的产品实体（懒加载）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", insertable = false, updatable = false)
    @JsonIgnore
    private DbuProduct product;

    /**
     * 字段中文名称
     */
    @Column(name = "field_name_cn", nullable = false, length = 200)
    private String fieldNameCn;

    /**
     * 字段英文名称（自动翻译或手动维护）
     */
    @Column(name = "field_name_en", nullable = false, length = 200, unique = true)
    private String fieldNameEn;

    /**
     * 字段类型（通用类型）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "field_type", nullable = false, length = 50)
    private ColumnType fieldType;

    /**
     * 字段长度
     */
    @Column(name = "field_length")
    private Integer fieldLength;

    /**
     * 小数位数
     */
    @Column(name = "field_scale")
    private Integer fieldScale;

    /**
     * 是否必填
     */
    @Column(name = "is_required", nullable = false)
    private Boolean isRequired = false;

    /**
     * 是否主键
     */
    @Column(name = "is_primary_key", nullable = false)
    private Boolean isPrimaryKey = false;

    /**
     * 是否唯一
     */
    @Column(name = "is_unique", nullable = false)
    private Boolean isUnique = false;

    /**
     * 默认值
     */
    @Column(name = "default_value", length = 200)
    private String defaultValue;

    /**
     * 字段注释
     */
    @Column(name = "field_comment", length = 500)
    private String fieldComment;

    /**
     * 是否启用
     */
    @Column(name = "is_enabled", nullable = false)
    private Boolean isEnabled = true;

    /**
     * 使用次数（统计）
     */
    @Column(name = "usage_count")
    private Integer usageCount = 0;
}